package com.croot.infra.${projectFlag}.adapter.rdb.impl;

import com.croot.infra.common.exception.RepositoryException;
import com.croot.infra.${projectFlag}.adapter.rdb.${entityName}Repository;
import com.croot.infra.${projectFlag}.adapter.rdb.consts.ErrorCode;
import com.croot.infra.${projectFlag}.adapter.rdb.mapper.${entityName}Mapper;
import com.croot.infra.${projectFlag}.adapter.rdb.po.${entityName}PO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ${entityName}RepositoryImpl implements ${entityName}Repository {

    @Autowired
    private ${entityName}Mapper ${entityParamName}Mapper;

    @Override
    public void insert(${entityName}PO ${entityParamName}PO) throws RepositoryException {
        try {
            ${entityParamName}Mapper.insert(${entityParamName}PO);
        } catch (Exception e) {
            log.error("表:${tableName}新增数据异常,详细错误: ", e);
            throw new RepositoryException(ErrorCode.SAVE_${upperTableName}_FAILED.getCode(),
                    ErrorCode.SAVE_${upperTableName}_FAILED.getMsg());
        }
    }

    @Override
    public void update(${entityName}PO ${entityParamName}PO) throws RepositoryException {
        try {
            ${entityParamName}Mapper.update(${entityParamName}PO);
        } catch (Exception e) {
            log.error("表:${tableName}修改数据异常,详细错误: ", e);
            throw new RepositoryException(ErrorCode.SAVE_${upperTableName}_FAILED.getCode(),
                    ErrorCode.SAVE_${upperTableName}_FAILED.getMsg());
        }
    }

    @Override
    public ${entityName}PO getById(Long id) throws RepositoryException {
        try {
            ${entityName}PO ${entityParamName}PO = new ${entityName}PO();
            ${entityParamName}PO.setId(id);
            return ${entityParamName}Mapper.selectById(${entityParamName}PO);
        } catch (Exception e) {
            log.error("表:${tableName}获取数据异常,详细错误: ", e);
            throw new RepositoryException(ErrorCode.QUERY_${upperTableName}_FAILED.getCode(),
                    ErrorCode.QUERY_${upperTableName}_FAILED.getMsg());
        }
    }

    @Override
    public List<${entityName}PO> list(${entityName}PO ${entityParamName}PO) throws RepositoryException {
        try {
            return ${entityParamName}Mapper.select(${entityParamName}PO);
        } catch (Exception e) {
            log.error("查询表:${tableName}的列表数据异常,详细错误: ", e);
            throw new RepositoryException(ErrorCode.QUERY_${upperTableName}_FAILED.getCode(),
                    ErrorCode.QUERY_${upperTableName}_FAILED.getMsg());
        }
    }

    @Override
    public Long count(${entityName}PO ${entityParamName}PO) throws RepositoryException {
        try {
            return ${entityParamName}Mapper.count(${entityParamName}PO);
        } catch (Exception e) {
            log.error("计算表:${tableName}的记录数异常,详细错误: ", e);
            throw new RepositoryException(ErrorCode.COUNT_${upperTableName}_FAILED.getCode(),
                    ErrorCode.COUNT_${upperTableName}_FAILED.getMsg());
        }
    }

    @Override
    public void delete(${entityName}PO ${entityParamName}PO) throws RepositoryException {
        try {
            ${entityParamName}Mapper.delete(${entityParamName}PO);
        } catch (Exception e) {
            log.error("删除表:${tableName}中的数据异常,详细错误: ", e);
            throw new RepositoryException(ErrorCode.DELETE_${upperTableName}_FAILED.getCode(),
                    ErrorCode.DELETE_${upperTableName}_FAILED.getMsg());
        }
    }
}